<?php
use Entities\Terreno;

include_once '../templates/topo.php';

$id= '';

if(isset($_REQUEST['id']))
	$id = $_REQUEST['id'];
	
$terreno = $em->find("Entities\Terreno", $id);
if(empty($terreno))
	$terreno = new Entities\Terreno();
	
?>

	<div class="tabs center">
			<ul>
				<li><a href="#tabs-1">Terrenos</a></li>
			</ul>
			<div id="tabs-1">
				<div cols='4'>
					<div class='field' >
						<label>Rua</label>
						<input type="hidden" value="<?php echo $id;?>" id='hidId' onkeypress="mascara(this,maiusculo)" />
						<input type='text' id='txtRua' value='<?php echo $terreno->getRua();?>' size='30' onkeypress="mascara(this,maiusculo)" />
					</div>
					<div class='field'>
						<label>Nro</label>
						<input type='text' id='txtNro' value='<?php echo $terreno->getNumero();?>' size='12' />
					</div>
					<div class='field'>
						<label>Quadra</label>
						<input type='text' id='txtQuadra' value='<?php echo $terreno->getQuadra();?>' size='12' />
					</div>
					<div class='field'>
						<label>Bairro</label>
						<select id="selBairro">
							<option value="0">Selecione</option>
							<option value="Alamedas" <?php if($terreno->getBairro() == 'Alamedas'){echo "selected='selected'";} ?> >Alamedas</option>
							<option value="Bela vista" <?php if($terreno->getBairro() == 'Bela vista'){echo "selected='selected'";} ?> >Bela vista</option>
							<option value="Paises" <?php if($terreno->getBairro() == 'Paises'){echo "selected='selected'";} ?> >Paises</option>
						</select>
					</div>
					
				</div>
			</div>
	
			<div class='footer'>
				<button id='btnSalvar'>Salvar</button>
				<script type="text/javascript">
					$("#btnSalvar").click(function(){
						if($('#txtRua').val() == '' || $('#txtNro').val() == '' || $('#txtQuadra').val() == '' || $('#selBairro').val() == '0'){
							alert('Favor preencher todos os campos!', 'Atencao');
						
						}else{
						
							utils.ajax('terreno/salvar', {
								id: $("#hidId").val(),
								rua: $('#txtRua').val(),
								numero: $('#txtNro').val(),
								quadra: $('#txtQuadra').val(),
								bairro: $('#selBairro').val()									
									
							}, function(xml){
									erro = $(xml).find('error').text();
									if(parseInt(erro) == "0"){
										msg = "O terreno foi salvo com sucesso!";
	
										$("#hidId").val('');
										$('#txtRua').val('');
										$('#txtNro').val('');
										$('#txtQuadra').val('');	
										$('#selBairro').val('0');
										$("#selBairro").selectmenu();								
										
									}
									else{
										msg = "O terreno NAO foi salvo com sucesso!";
									}
//									alert(msg, 'Terreno');

									parent.$("body").trigger(funcaoRetorno, msg);
							});
						}
					});
				</script>
			</div>
	</div>

<?php 
if(!$popup)
	include_once '../templates/footer.php';
?>